﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 紧缩场反射面装调测试系统.Controll
{
    /// <summary>
    /// S_Parameter.xaml 的交互逻辑
    /// </summary>
    /// 
    [INotifyPropertyChanged]
    public partial class S_Parameter : UserControl
    {
        public S_Parameter()
        {
            InitializeComponent();
        }


        public int PortCount
        {
            get { return (int)GetValue(PortCountProperty); }
            set { SetValue(PortCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PortCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PortCountProperty =
            DependencyProperty.Register("PortCount", typeof(int), typeof(S_Parameter), new PropertyMetadata(0, PortCountChangeCallback));

        private static void PortCountChangeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            S_Parameter s_Parameter = (S_Parameter)d;
            if ((int)e.NewValue > 9 || (int)e.NewValue < 2)
            {
                s_Parameter.ResetPort(2);

            }
            else
            {
                s_Parameter.ResetPort((int)e.NewValue);
            }
        }

        public string SParameter
        {
            get { return (string)GetValue(SParameterProperty); }
            set { SetValue(SParameterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SParameter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SParameterProperty =
            DependencyProperty.Register("SParameter", typeof(string), typeof(S_Parameter), new PropertyMetadata("S21", SParameterChangeCallback));

        private static void SParameterChangeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            S_Parameter s_Parameter = (S_Parameter)d;
            s_Parameter.SetSParameter((string)e.NewValue);
        }


        [ObservableProperty]
        public string? iD;

        public string ID1
        {
            get { return iD == null ? "" : "" + "_1"; }
        }
        public string ID2
        {
            get { return iD == null ? "" : "" + "_2"; }
        }

        public ObservableCollection<Item> RK { get; set; } = new ObservableCollection<Item>();

        public ObservableCollection<Item> CK { get; set; } = new ObservableCollection<Item>();


        public void ResetPort(int Count)
        {
            RK.Clear();
            CK.Clear();
            for (int i = 0; i < Count; i++)
            {
                RK.Add(new Item() { Name = (i + 1).ToString() });
                CK.Add(new Item() { Name = (i + 1).ToString() });
            }

            RK[0].Selected = true;
            CK[1].Selected = true;
        }

        public void SetSParameter(string SParameter)
        {
            int rkindex = 0;
            int ckindex = 0;
            var S = int.Parse(SParameter.ToUpper().Replace("S", "")); ;
            if (S < 11 || S > 99)
            {
                rkindex = ckindex = 0;
            }

            try
            {
                rkindex = S % 10 - 1;
                ckindex = S / 10 - 1;
            }
            catch
            {
                rkindex = ckindex = 0;
            }


            if (rkindex < 0 || rkindex > PortCount - 1)
            {
                rkindex = 0;
            }
            if (ckindex < 0 || ckindex > PortCount - 1)
            {
                ckindex = 0;
            }

            for (int i = 0; i < RK.Count; i++)
            {
                if (i == rkindex)
                {
                    RK[i].Selected = true;
                }
                else
                {
                    RK[i].Selected = false;
                }

                if (i == ckindex)
                {
                    CK[i].Selected = true;
                }
                else
                {
                    CK[i].Selected = false;
                }
            }

        }



        [RelayCommand]
        void SelectedChange()
        {
            string? r = "";
            string? c = "";
            for (int i = 0; i < RK.Count; i++)
            {
                if (RK[i].Selected)
                {
                    r = RK[i].Name;
                    break;
                }
            }
            for (int i = 0; i < CK.Count; i++)
            {
                if (CK[i].Selected)
                {
                    c = CK[i].Name;
                    break;
                }
            }
            if (string.IsNullOrEmpty(r) || string.IsNullOrEmpty(c))
            {
                throw new Exception("Select S Parameter failed!");
            }
            SParameter = "S" + c + r;
        }

        private void TreeViewItem_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)

        {

            e.Handled = true;

        }
    }

    public sealed partial class Item : ObservableObject
    {
        [ObservableProperty]
        public bool selected;
        [ObservableProperty]
        public string? name;
    }
}
